package com.pcbly.web.components.taglib.tag;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.components.Component;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;
import org.apache.struts2.util.TextProviderHelper;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.opensymphony.xwork2.util.ValueStack;
import com.pcbly.web.components.taglib.Head;

public class HeadTag extends ComponentTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4807208319559833567L;
	
	private String adapter;
	private String debug;
	private String extLocation;
	private String theme;
	private String locale;
	private String msgTarget;
	private String smProvider;
	
	@Override
	public Component getBean(ValueStack stack, HttpServletRequest request,
			HttpServletResponse reponse) {
		// TODO Auto-generated method stub
		return new Head(stack);
	}
	
	protected String getObjectValue(String obj) {
		if (obj != null && obj.startsWith("%{") && obj.endsWith("}")) {
			obj = obj.substring(2, obj.length() - 1);
			obj = String.valueOf(this.getStack().findValue(obj));
		} else {
			String tv = null;
			tv = obj;
			obj = String.valueOf(this.getStack().findValue(obj));
			if (obj == null || "".equals(obj)) {
				obj = TextProviderHelper.getText(tv, tv, this.getStack());
			}
		}
		return obj;
	}
	protected String getStringValue(String obj) {
		if (obj != null && obj.startsWith("%{") && obj.endsWith("}")) {
			obj = obj.substring(2, obj.length() - 1);
			obj = String.valueOf(this.getStack().findValue(obj));
		}
		return obj;
	}
	protected void populateParams() {
		super.populateParams();
		adapter=getStringValue(adapter);
		debug=getStringValue(debug);
		extLocation=getStringValue(extLocation);
		if(extLocation==null||"".equals(extLocation)){
			Map<String, Object> cont = this.getStack().getContext();
			StrutsRequestWrapper request = (StrutsRequestWrapper) cont
					.get(StrutsStatics.HTTP_REQUEST);
			String url = request.getContextPath();
			extLocation=url;
		}
		theme=getStringValue(theme);
		locale=getStringValue(locale);
		msgTarget=getStringValue(msgTarget);
		smProvider=getStringValue(smProvider);
		Head head = (Head)component; 
		head.setAdapter(adapter);
		head.setDebug(debug);
		head.setExtLocation(extLocation);
		head.setTheme(theme);
		head.setLocale(locale);
		head.setMsgTarget(msgTarget);
		head.setSmProvider(smProvider);
		
	}

	public String getAdapter() {
		return adapter;
	}

	public void setAdapter(String adapter) {
		this.adapter = adapter;
	}

	public String getDebug() {
		return debug;
	}

	public void setDebug(String debug) {
		this.debug = debug;
	}

	public String getExtLocation() {
		return extLocation;
	}

	public void setExtLocation(String extLocation) {
		this.extLocation = extLocation;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getLocale() {
		return locale;
	}

	public void setLocale(String locale) {
		this.locale = locale;
	}


	public String getMsgTarget() {
		return msgTarget;
	}

	public void setMsgTarget(String msgTarget) {
		this.msgTarget = msgTarget;
	}

	public String getSmProvider() {
		return smProvider;
	}

	public void setSmProvider(String smProvider) {
		this.smProvider = smProvider;
	}
}
